home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / login.zip / GETFLD.C < prev    next >
C/C++ Source or Header  |  1991-02-03  |  927b  |  54 lines

  1. #include <stdio.h>
  2. #ifdef MAIN
  3. char *getfld();
  4. main()
  5. {
  6.     int x;
  7.     char b[] = "root:foo-bar:shell";
  8.     char c[] = "root::shell";
  9.     char    *d;
  10.  
  11.     printf("b=%s\n", b);
  12.     for (x = 0; x < 3; ++x)
  13.         printf("field:%d = [%s]\n", x, getfld(b, x, ':'));
  14.  
  15.     printf("c=%s\n", c);
  16.     for (x = 0; x < 3; ++x)
  17.         printf("field:%d = [%s]\n", x, getfld(c, x, ':'));
  18. }
  19. #endif
  20. char    *
  21. getfld(a, b, c)
  22. char    *a, c;
  23. int     b;
  24. {
  25.     char    *p, *e;
  26.  
  27.     p = a;
  28.     while (*a != '\0') {
  29.         if (*a == c) {
  30.             if (b == 0) {
  31.                 *a = '\0';
  32.                 if ((e = (char *)malloc(strlen(p)+1)) == NULL)
  33.                     return (NULL);
  34.                 strcpy(e, p);
  35.                 *a = c;
  36.                 return(e);
  37.             }
  38.             *a++;
  39.             p = a;
  40.             --b;
  41.             continue;
  42.         }
  43.         *a++;
  44.     }
  45.     if (b == 0) {
  46.         if ((e = (char *)malloc(strlen(p)+1)) == NULL)
  47.             return (NULL);
  48.         strcpy(e, p);
  49.         return(e);
  50.     }
  51.     return(NULL);
  52. }
  53.  
  54.